package com.jinglin.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.enums.ResponseCodeEnum;
import com.jinglin.entity.po.VideoDanmu;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.exception.BusinessException;
import com.jinglin.service.VideoDanmuService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.web.annotation.GlobalInterceptor;
import org.apache.coyote.Response;
import org.apache.lucene.search.similarities.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 视频弹幕 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-03-24
 */
@RestController
@RequestMapping("/danmu")
public class VideoDanmuController extends ABaseController {
    @Autowired
    private VideoDanmuService videoDanmuService;
    @Autowired
    private VideoInfoService videoInfoService;
    @PostMapping("/postDanmu")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO postDanmu(VideoDanmu videoDanmu){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        videoDanmu.setUserId(tokenUserInfoDto.getUserId());
        videoDanmu.setPostTime(LocalDateTime.now());
        videoDanmuService.saveVideoDanmu(videoDanmu);
        return getSuccessResponseVO(null);
    }
    @PostMapping("/loadDanmu")
    public ResponseVO loadDanmu(@NotEmpty String fileId,@NotEmpty String videoId){
        VideoInfo videoInfo = videoInfoService.getById(videoId);
        if (videoInfo == null){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (videoInfo.getInteraction() != null && videoInfo.getInteraction().contains(Constants.ONE.toString())) {
            return getSuccessResponseVO(new ArrayList<>());
        }

        LambdaQueryWrapper<VideoDanmu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoDanmu::getFileId,fileId);
        wrapper.eq(VideoDanmu::getVideoId,videoId);
        List<VideoDanmu> list = videoDanmuService.list(wrapper);
        return getSuccessResponseVO(list);
    }
}

